package com.topmap.controller;

import com.topmap.controller.viewobject.FeiyiPeopleVO;
import com.topmap.dao.FeiyiPeopleDOMapper;
import com.topmap.response.CommonReturnType;
import com.topmap.service.FeiyiPeopleService;
import com.topmap.service.model.FeiyiPeopleModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: MaHuadong
 * @Date: 2019/7/3 10:56
 * @Version 1.0
 */
@Controller("/feiyipeople")
@RequestMapping("/feiyipeople")
//允许跨域
@CrossOrigin(origins = {"*"},allowCredentials = "true")
public class FeiyiPeopleController {


    @Autowired
    private FeiyiPeopleService feiyiPeopleService;
    @Autowired
    private FeiyiPeopleDOMapper feiyiPeopleDOMapper;

    //通过非遗传承人名字查询非遗
    //通过非遗名称查询非遗
    @RequestMapping(value = "/get",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType getFeiyiPeople(@RequestParam(name = "people") String people){
        //首先根据非遗名称获取非遗model
        FeiyiPeopleModel feiyiPeopleModel =feiyiPeopleService.getFeiyiPeopleByPeople(people);

        //把model转换为供前端展示的VO层
        FeiyiPeopleVO feiyiPeopleVO =convertVOFromModel(feiyiPeopleModel);

        return CommonReturnType.creat(feiyiPeopleVO);
    }


    //查询所有非遗传承人信息
    //查询所有的非遗
    @RequestMapping(value = "/listfeiyipeople",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType getAllFeiyi(){
        List<FeiyiPeopleModel> feiyiPeopleModelList=feiyiPeopleService.listFeiyiPeople();

        //把model转换为供前端展示的vo(涉及到list类型数据的转换)还是使用java8的stream的api
        List<FeiyiPeopleVO> feiyiPeopleVOList=feiyiPeopleModelList.stream().map(feiyiPeopleModel -> {
            FeiyiPeopleVO feiyiPeopleVO=this.convertVOFromModel(feiyiPeopleModel);
            return feiyiPeopleVO;
            //转换为list类型，使得可以返回list类型的数据
        }).collect(Collectors.toList());

        //最后将返回给前端的productvo返回给前端
        return CommonReturnType.creat(feiyiPeopleVOList);
    }




    private FeiyiPeopleVO convertVOFromModel(FeiyiPeopleModel feiyiPeopleModel) {
        if (feiyiPeopleModel==null){
            return null;
        }

        FeiyiPeopleVO feiyiPeopleVO =new FeiyiPeopleVO();

        //copy
        BeanUtils.copyProperties(feiyiPeopleModel,feiyiPeopleVO);

        return feiyiPeopleVO;
    }

}
